#include "FileBrowser.h"
#include "FileCheckBox.h"

QString un_utf8(const char*ChineseString);

FileCheckBox::FileCheckBox(const QString &Title,const QString &dir,ZMainWidget *parent , char const *name , unsigned int flags)
  :ZMainWidget(false,dir, parent, name, flags)
{
	((QLabel* )getTitleBarWidget())->setText(Title);

	CheckBox = new QCheckBox(tr("All"),getTitleBarWidget(),"CheckBox");
	CheckBox->move(200,3);
	CheckBox->resize(20,20);
	connect(CheckBox, SIGNAL( clicked() ),this, SLOT(isChecked()));

	FileCheck = new FileBrowser("ezx_util_list",this);
	FileCheck->setListBoxType(CheckboxListItem);
	FileCheck->setDir(dir);
	setContentWidget(FileCheck);

	UTIL_CST* cst = new UTIL_CST(this,tr("OK"));
	setCSTWidget(cst);
	cst->getRightBtn()->setResourceID("CST_Back");
	connect(cst->getRightBtn(), SIGNAL(clicked()), this, SLOT(close()));
	connect(cst->getMidBtn(), SIGNAL(clicked()), this , SLOT(Send()));

	QPopupMenu* mSelectMenu = new QPopupMenu(this, NULL);
	mSelectMenu->insertItem(tr("All"), this, SLOT(selectAll()),0 );
	mSelectMenu->insertItem(tr("Undo"), this, SLOT(deselectAll()),0 );
	mSelectMenu->insertItem(tr("Invert"), this, SLOT(invertSelection()),0 );
	cst->getLeftBtn()->setPopup(mSelectMenu);

}
FileCheckBox::~FileCheckBox()
{
}
void FileCheckBox::selectAll()
{
  unsigned int i = 0;
  for (i = 0; i < (FileCheck->count() + 1); ++i){
    FileCheck->setChecked((FileBrowserItem*)FileCheck->item(i), true);
  }
}
void FileCheckBox::deselectAll()
{
  unsigned int i = 0;
  for (i = 0; i < (FileCheck->count() + 1); ++i){
    FileCheck->setChecked((FileBrowserItem*)FileCheck->item(i), false);
  }
}
void FileCheckBox::invertSelection()
{
  unsigned int i = 0;
  for (i = 0; i < (FileCheck->count() + 1); ++i){
  FileBrowserItem *item = (FileBrowserItem*)FileCheck->item(i);
   if (item) FileCheck->setChecked(item, !item->isChecked());
  }
}
void FileCheckBox::isChecked()
{
  CheckBox->isChecked() ? selectAll() : deselectAll();
}
void FileCheckBox::Send()
{
  FileList = FileCheck->SelectedList(All);
  emit SendFileList(FileList);
  close();
}
